<template>
  <div class="app-container">
    <div class="filter-container">
      <statisSearchBar :start-date="searchForm.startDate" :end-date="searchForm.endDate" :showp-pf-channel="true" @transferFilter="handleFilter" />
    </div>
    <div style="width: 280px; margin: 0 auto;">总充值金额：<span class="red_num">{{ amount }}</span> {{ '\xa0' }} {{ '\xa0' }}总充值人数：<span class="red_num">{{ total }}</span></div>
    <div ref="echartsContainer" style="width: 100%; height: 400px;"></div>
    <el-table v-loading="listLoading" :data="list" class="mt-15" border>
      <el-table-column label="时间" prop="date" width="100px" align="center"></el-table-column>
      <el-table-column label="充值金额" prop="amount" width="100px" align="center"></el-table-column>
      <el-table-column label="充值人数" prop="num"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import * as echarts from 'echarts'
import StatisSearchBar from '@/components/StatisSearchBar'
import { getRechargeStatis } from '@/api/statis'
import { formatCurrency } from '@/common/public'

export default {
  name: 'OnlineList',
  components: { StatisSearchBar },
  data() {
    return {
      list: [],
      hourList: [],
      amountData: [],
      amount: 0,
      total: 0,
      numData: [],
      chartxAxisData: [],
      listLoading: false,
      searchForm: {
        startDate: new Date(Date.now() - 86400000 * 10),
        endDate: new Date()
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    handleFilter(query) {
      this.searchForm = query
      this.getList()
    },
    async getList() {
      this.listLoading = true
      const res = await getRechargeStatis(this.searchForm)
      this.listLoading = false
      this.list = res.data.items
      this.amount = res.data.amount
      this.total = res.data.total
      this.hanleChart()
    },

    hanleChart() {
      this.amountData = []
      this.numData = []
      this.chartxAxisData = []

      this.list.forEach(v => {
        this.chartxAxisData.push(v.date)
        this.amountData.push((v.amount))
        this.numData.push(v.num)
      })
      console.log(this.chartxAxisData)
      console.log(this.amountData)
      console.log(this.numData)
      var myChart = echarts.init(this.$refs.echartsContainer)
      myChart.setOption(
        {
          tooltip: {
            trigger: 'axis',
            axisPointer: {
              type: 'cross',
              crossStyle: {
                color: '#999'
              }
            }
          },
          legend: {},
          xAxis: {
            type: 'category',
            data: this.chartxAxisData
          },
          yAxis: [
            {
              type: 'value',
              name: '充值金额',
              splitLine: { show: false },
              axisLabel: {
                formatter: (value) => `${formatCurrency(value)} 元`
              }
            },
            {
              type: 'value',
              name: '充值人数',
              splitLine: { show: false },
              axisLabel: {
                formatter: '{value} 人'
              }
            }
          ],
          series: [
            {
              data: this.amountData,
              type: 'bar',
              name: '充值金额',
              smooth: true,
              barWidth: '30%',
              label: {
                show: true, // 显示标签
                position: 'top', // 标签显示在点的上方
                formatter: (params) => `${formatCurrency(params.value)}`
              }
            },
            {
              data: this.numData,
              type: 'line',
              name: '充值人数',
              smooth: true,
              barWidth: '30%',
              yAxisIndex: 1,
              label: {
                show: true, // 显示标签
                position: 'top', // 标签显示在点的上方
                formatter: '{c}' // 标签内容为数据值
              }
            }
          ]
        }
      )
    }
  }
}
</script>

<style>
.red_num {
  font-weight: bold;color: red
}
</style>
